<template>
  <div>
    <el-dialog
      title="编辑"
      :visible="visible"
      width="80%"
      @close="visible = false"
      :close-on-click-modal="false"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="100px"
        class="demo-form"
        label-position="top"
        size="mini"
        style="width: 100%"
        v-loading="loading"
      >
        <el-form-item
          label="表单名称"
          prop="workflowId"
          :label-width="defaultLabelWidth"
        >
          <el-select
            filterable
            style="width: 100%"
            v-model="form.workflowId"
            placeholder="请选择"
            default-first-option
            disabled
          >
            <el-option
              v-for="item in allWorkflows"
              :key="item.id + item.activeVersion"
              :label="item.name + ':' + item.activeVersion"
              :value="item.id + ':' + item.activeVersion"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="标题"
          prop="title"
          :label-width="defaultLabelWidth"
        >
          <el-input
            disabled
            size="mini"
            type="text"
            v-model="form.title"
          ></el-input>
        </el-form-item>

        <el-form-item label="表单数据" prop="title"> </el-form-item>
      </el-form>
      <el-card shadow="never">
        <controlPanel ref="controlPanel" :isEdit="true"></controlPanel>
      </el-card>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="confirm" size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import controlPanel from "./../formDesign/controlPanel.vue";
import defaultToVfrom from "@/views/workflows/formDesign/designContentConverters/defaultToVfrom";
export default {
  components: { controlPanel },
  props: {
    value: {
      id: 1,
    },
  },
  watch: {
    value: {
      handler() {
        //this.load();
      },
      deep: true,
    },
    visible(newVal, oldVal) {
      if (!newVal && oldVal) this.hide();
    },
  },
  model: {
    prop: "value",
    event: "change",
  },
  data() {
    return {
      loading: false,
      visible: false,
      defaultLabelWidth: "150px",
      form: {
        title: "",
        value: "",
      },
      rules: {
        title: [{ required: true, message: "请输入", trigger: "blur" }],
        workflowId: [{ required: true, message: "请选择", trigger: "blur" }],
      },
      allWorkflows: [],
      formDesign: {},
      workTaskForm: {
        entityFullName: "dynamicForm",
        entityKeyValue: "",
        formData: "{}",
        workTaskStatus: "",
        name: "",
        workflowId: {},
      },
    };
  },
  methods: {
    confirm() {
      this.commit();
    },
    show(value) {
      this.visible = true;
      this.getAllWorkflowsWithVersion();
      if (value) this.loadData(value);
    },
    hide() {
      this.visible = false;
      this.form = {
        name: "",
      };
    },
    loadData(value) {
      this.loading = true;
      this.getWorkTask(value);
      this.loading = false;
    },
    commit() {
      const _this = this;
      this.updateFormData();
    },
    getAndRendWithFormDesign(workTask) {
      this.loading = true;
      let keyValue = {};
      try {
        keyValue = JSON.parse(workTask.entityKeyValue);
      } catch (e) {
        keyValue = {};
      }
      let formData = {};
      try {
        formData = JSON.parse(workTask.formData);
      } catch (e) {
        formData = {};
      }

      this.$refs.controlPanel.setValue([], formData);

      const formDesignId = {
        ...keyValue,
        version: keyValue.versionNo,
      };

      this.$store
        .dispatch("dynamicFormDesign/getFormDesignVersion", formDesignId)
        .then((res) => {
          this.loading = false;
          this.formDesign = res;
          if (res && res.designContent) {
            if (res.designFramework != "vform") {
              return defaultToVfrom(res.designContent);
            } else {
              return JSON.parse(res.designContent || "{}");
            }
          } else return null;
        })
        .then((controls) => {
          this.loading = false;
          //this.$set(this.form, "formData", JSON.parse(res.formData || "{}"));
          this.$refs.controlPanel.setValue(controls, formData);
        });
    },

    getWorkTask(id) {
      this.loading = true;
      let _this = this;
      let dispatchPath = "workflow/getWorkTask";
      this.$store
        .dispatch(dispatchPath, { id })
        .then((res) => {
          this.form.id = res.id;
          this.form.title = res.name;

          this.form.workflowId =
            res.workflowId.id + ":" + res.workflowId.versionNo;

          this.getAndRendWithFormDesign(res);
          this.loading = false;
          return res;
        })
        .catch((e) => {
          this.loading = false;
        });
    },
    getAllWorkflowsWithVersion() {
      this.loading = true;
      this.$store
        .dispatch("workflow/getAllWorkflowsWithVersion", this.form)
        .then((res) => {
          this.allWorkflows = res;
          this.loading = false;
        });
    },
    updateFormData() {
      this.loading = true;
      const data = {
        id: this.form.id,
        formData: JSON.stringify(this.$refs.controlPanel.getValue()),
      };
      this.$store
        .dispatch("workflow/updateWorkTaskFormData", data)
        .then((res) => {
          this.loading = false;
          this.hide();
        })
        .catch((e) => {
          this.loading = false;
        });
    },
  },
};
</script>

<style></style>
